home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / kant-generator-04-c / Kant ƒ / Shell ƒ / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  11.7 KB  |  397 lines  |  [TEXT/MMCC]

  1. #include "graphics.h"
  2. #include "main.h"
  3. #include "apple events.h"
  4. #include "integrity.h"
  5. #include "about.h"
  6. #include "menus.h"
  7. #include "prefs.h"
  8. #include "environment.h"
  9. #include "error.h"
  10. #include "print meat.h"
  11. #include "kant.h"
  12. #include "program globals.h"
  13. #include "AppleEvents.h"
  14. #include "EPPC.h"
  15.  
  16. static    short            gTheCurrentModifiers;
  17.  
  18. void main(void)
  19. {
  20.     Boolean            programIntegrityVerified;
  21.     Boolean            programIntegritySet;
  22.     
  23.     /* do integrity check before anything else; see integrity.c for details */
  24.     programIntegrityVerified=DoIntegrityCheck(&programIntegritySet);
  25.     
  26.     /* standard program initialization stuff */
  27.     MaxApplZone();    
  28.     InitGraf(&qd.thePort);
  29.     InitFonts();
  30.     FlushEvents(everyEvent, 0);
  31.     InitWindows();
  32.     InitMenus();
  33.     TEInit();
  34.     InitDialogs(0L);
  35.     InitCursor();
  36.     GetDateTime((unsigned long*)&qd.randSeed);
  37.     
  38.     if (!InitTheEnvironment())            /* gestalt checks and variable initialization */
  39.         HandleError(kSystemTooOld, TRUE);        /* less than system 4.1 */
  40.     
  41.     if (!programIntegrityVerified)    /* integrity check failed */
  42.         HandleError(kProgramIntegrityNotVerified, TRUE);
  43.     
  44.     if (programIntegritySet)    /* integrity check freshly installed */
  45.         HandleError(kProgramIntegritySet, FALSE);
  46.     
  47.     PrefsError(PreferencesInit());    /* get prefs (create if necessary) */
  48.     
  49.     if (!InitTheGraphics())        /* initialize offscreen gworlds/bitmaps, etc */
  50.         HandleError(kNoMemoryAndQuitting, TRUE);
  51.     
  52.     if (!InitTheMenus())        /* get menus from .rsrc and draw menu bar */
  53.         HandleError(kProgramIntegrityNotVerified, TRUE);
  54.         
  55.     InitThePrinting();
  56.     
  57.     InitTheProgram();                /* program-specific initialization */
  58.     
  59.     EventLoop();                    /* where it all happens (see below) */
  60.     
  61.     ShutDownEnvironment(TRUE);        /* where it all ends (see below) */
  62.     
  63.     ExitToShell();
  64. }
  65.  
  66. void EventLoop(void)
  67. {
  68.     while (!gDone)    /* gDone set by choosing "Quit" menu item or by "quit" apple event */
  69.         HandleSingleEvent(TRUE);
  70. }
  71.  
  72. Boolean HandleSingleEvent(Boolean allowContextSwitching)
  73. {
  74.     EventRecord        theEvent;
  75.     short            i;
  76.     
  77.     if (!gCustomCursor)
  78.         SetCursor(&qd.arrow);        /* should set once every time through event loop */
  79.     HiliteMenu(0);            /* normalize menubar */
  80.     
  81.     gFrontWindowIsOurs=FALSE;
  82.     gFrontWindowIndex=0;
  83.     if (FrontWindow()!=0L)    /* if there's a front window, see if it's one of ours */
  84.     {
  85.         for (i=0; ((i<NUM_WINDOWS) && (!gFrontWindowIsOurs)); i++)
  86.         {
  87.             if (FrontWindow()==GetIndWindowPtr(i))    /* found one of ours, so SetPort */
  88.             {
  89.                 SetPort(GetIndWindowPtr(i));
  90.                 gFrontWindowIsOurs=TRUE;
  91.                 gFrontWindowIndex=i;
  92.             }
  93.         }
  94.     }
  95.         
  96.     /* get an event from the queue */
  97.     WaitNextEvent(everyEvent, &theEvent, gIsInBackground ? gBackgroundWaitTime : 
  98.         ((gKludgeIter<3) ? 0 : gForegroundWaitTime), 0L);
  99.     gTheCurrentModifiers=theEvent.modifiers;
  100.     
  101.     DispatchEvents(theEvent, allowContextSwitching);    /* handle the event we just got */
  102.     
  103.     return (theEvent.what!=nullEvent);
  104. }
  105.  
  106. short GetTheModifiers(void)
  107. {
  108.     return gTheCurrentModifiers;
  109. }
  110.  
  111. void DispatchEvents(EventRecord theEvent, Boolean allowContextSwitching)
  112. {
  113.     short            i;
  114.     Point            thisPoint;
  115.     short            index;
  116.     WindowPtr        theWindow;
  117.     Boolean            thisWindowIsOurs;
  118.     
  119.     thisWindowIsOurs=FALSE;
  120.     /* for update/activate events, see if the window in question is one of ours */
  121.     if ((theEvent.what==activateEvt) || (theEvent.what==updateEvt))
  122.     {
  123.         for (i=0; ((i<NUM_WINDOWS) && (!thisWindowIsOurs)); i++)
  124.             thisWindowIsOurs=((WindowPtr)theEvent.message==GetIndWindowPtr(i));
  125.     }
  126.  
  127.     if (thisWindowIsOurs)    /* for activate/update events, get window index */
  128.         index=(**((WindowDataHandle)GetWRefCon((WindowPtr)theEvent.message))).windowIndex;
  129.     else if (gFrontWindowIsOurs)    /* if front window is ours, get its window index */
  130.         index=gFrontWindowIndex;
  131.     else index=-1;
  132.     
  133.     switch (theEvent.what)
  134.     {
  135.         case nullEvent:    /* ain't nuthin' happenin' */
  136.             if (gKludgeIter<3)
  137.             {
  138.                 gKludgeIter++;
  139.             }
  140.             else
  141.             {
  142.                 if (gNeedToOpenWindow)
  143.                 {
  144.                     OpenTheIndWindow(kMainWindow);
  145.                     gNeedToOpenWindow=FALSE;
  146.                 }
  147.             }
  148.             
  149.             if (gFrontWindowIsOurs)        /* give control to window dispatch to handle null */
  150.                 CallIndDispatchProc(index, kNull, 0L);
  151.             break;
  152.         case mouseDown:    /* mouse button pressed */
  153.             HandleMouseDown(theEvent, allowContextSwitching);    /* see below for mousedown handling */
  154.             break;
  155.         case keyDown:    /* key pressed */
  156.         case autoKey:    /* key help down */
  157.             if (theEvent.modifiers & cmdKey)    /* handle as command-key equivalent */
  158.             {
  159.                 AdjustMenus();    /* just to be safe */
  160.                 /* get the menu ID + item and handle it as a menu choice */
  161.                 HandleMenu(MenuKey((char)(theEvent.message & charCodeMask)));
  162.             }
  163.             else if (gFrontWindowIsOurs)    /* --> window's dispatch for keydown */
  164.                 CallIndDispatchProc(index, kKeydown, theEvent.message);
  165.             break;
  166.         case diskEvt:    /* disk insert */
  167.             if (HiWord(theEvent.message)!=noErr)    /* bad disk inserted */
  168.             {
  169.                 DILoad();    /* load disk initialization package */
  170.                 SetPt(&thisPoint, 120, 120);
  171.                 DIBadMount(thisPoint, theEvent.message);    /* give format? dialog */
  172.                 DIUnload();    /* unload 'cuz we certainly don't need it */
  173.             }
  174.             break;
  175.         case updateEvt:    /* window update */
  176.             theWindow=(WindowPtr)theEvent.message;    /* which window? */
  177.             
  178.             BeginUpdate(theWindow);        /* means: "OK, we're dealing with this now" */
  179.             
  180.             if (thisWindowIsOurs)        /* one of ours?  see graphics.c */
  181.                 UpdateTheWindow(GetWindowDataHandle(index));
  182.             
  183.             EndUpdate(theWindow);        /* means: "OK, we're done updating now" */
  184.             break;
  185.         case activateEvt:    /* window activate or deactivate */
  186.             if (thisWindowIsOurs)        /* one of ours?  send message to window dispatch */
  187.                 CallIndDispatchProc(index, ((theEvent.modifiers&activeFlag)!=0) ?
  188.                     kActivate : kDeactivate, 0L);
  189.             break;
  190.         case osEvt:            /* suspend or resume program execution (switch in/out) */
  191.             if (((theEvent.message>>24)&0x0FF)==suspendResumeMessage)
  192.             {
  193.                 /* keep track of whether we're in the background or foreground */
  194.                 gIsInBackground=((theEvent.message&resumeFlag)==0);
  195.                 
  196.                 if (gFrontWindowIsOurs)        /* send activate/deactivate to front window */
  197.                     CallIndDispatchProc(index, gIsInBackground ? kDeactivate : kActivate, 0L);
  198.                 
  199.                 for (i=0; i<NUM_WINDOWS; i++)    /* send suspend/resume to all our */
  200.                     if (GetIndWindowPtr(i)!=0L)        /* windows that currently exist */
  201.                         CallIndDispatchProc(i, gIsInBackground ? kSuspend : kResume, 0L);
  202.                 
  203.                 /* if we just came into the foreground and we have a pending error,
  204.                    now's the time to display it */
  205.                 if ((!gIsInBackground) && (gPendingResultCode!=allsWell))
  206.                 {
  207.                     if (gHasNotificationManager)
  208.                         NMRemove(&gMyNotification);        /* remove notification request */
  209.                     HandleError(gPendingResultCode, FALSE);    /* display alert, see error.c */
  210.                     gPendingResultCode=allsWell;        /* ...now it is */
  211.                 }
  212.             }
  213.             break;
  214.         case kHighLevelEvent:    /* apple event */
  215.             AEProcessAppleEvent(&theEvent);        /* see apple events.c */
  216.             break;
  217.     }
  218. }
  219.  
  220. void HandleMouseDown(EventRecord theEvent, Boolean allowContextSwitching)
  221. {
  222.     WindowPtr        theWindow;
  223.     short            windowCode;
  224.     long            windSize;
  225.     GrafPtr            oldPort;
  226.     Rect            sizeRect;
  227.     short            index;
  228.     unsigned long    dummy;
  229.     Point            theLocalPoint;
  230.     Boolean            thisWindowIsOurs;
  231.     ExtendedWindowDataHandle    theData;
  232.     
  233.     windowCode=FindWindow(theEvent.where, &theWindow);    /* which window? */
  234.  
  235.     thisWindowIsOurs=FALSE;
  236.     /* find out if the target window was one of ours */
  237.     if (theWindow!=0L)
  238.         for (index=0; ((index<NUM_WINDOWS) && (!thisWindowIsOurs)); index++)
  239.         {
  240.             theData=GetWindowDataHandle(index);
  241.             thisWindowIsOurs=(theWindow==GetWindowPtr(theData));
  242.         }
  243.         
  244.     if (thisWindowIsOurs)    /* if target window is one of ours, get window data struct */
  245.     {
  246.         index=GetWindowIndex(theData);
  247.     }
  248.     else index=-1;
  249.     
  250.     switch (windowCode)
  251.     {
  252.         case inMenuBar:        /* in menu bar; let system take over */
  253.             AdjustMenus();
  254.             HandleMenu(MenuSelect(theEvent.where));
  255.             break;
  256.         case inContent:        /* in window content */
  257.             if (FrontWindow() != theWindow)        /* maybe switch to different window */
  258.             {
  259. //                if ((gInProgress) && (theWindow!=GetIndWindowPtr(kProgressWindow)))
  260.                 if (0)
  261.                 {
  262.                     SysBeep(7);
  263.                     return;
  264.                 }
  265.                 
  266.                 SelectWindow(theWindow);
  267.             }
  268.             else if (gFrontWindowIsOurs)    /* inform window dispatch of mousedown */
  269.             {
  270.                 theLocalPoint=theEvent.where;
  271.                 GlobalToLocal(&theLocalPoint);
  272.                 dummy=theLocalPoint.h;        /* all this fiddling is so we can pass */
  273.                 dummy=dummy<<16;            /* the point (two integers) to the */
  274.                 dummy+=theLocalPoint.v;        /* dispatch procedure in an unsigned long */
  275.                 CallIndDispatchProc(index, kMousedown, dummy);    /* go for it */
  276.             }
  277.             break;
  278.         case inSysWindow:    /* in system window (desk accessory) */
  279.             if (allowContextSwitching)
  280.                 SystemClick(&theEvent, theWindow);    /* let the system deal with it */
  281.             break;
  282.         case inDrag:        /* in drag _region_, that is */
  283.             /* the accepted way to draw a window */
  284. //            if ((gInProgress) && (theWindow!=GetIndWindowPtr(kProgressWindow)))
  285.             if (0)
  286.             {
  287.                 SysBeep(7);
  288.                 return;
  289.             }
  290.             DragWindow(theWindow, theEvent.where, &((**GetGrayRgn()).rgnBBox));
  291.             if (thisWindowIsOurs)    /* update window bounds in window data struct */
  292.             {
  293.                 if (CallDispatchProc(theData, kDrag, 0L)==kFailure)
  294.                 {
  295.                     SetWindowData_windowBounds(theData, 
  296.                         (*(((WindowPeek)GetWindowPtr(theData))->contRgn))->rgnBBox);
  297.                     SetWindowData_windowPos(theData,
  298.                         GetWindowBoundsTopLeft((WindowDataHandle)theData));
  299.                     SetWindowData_windowPosInitted(theData, TRUE);
  300.                 }
  301.             }
  302.             break;
  303.         case inGoAway:        /* close box */
  304.             /* the accepted way to track a close box attempt */
  305. //            if ((gInProgress) && (theWindow!=GetIndWindowPtr(kProgressWindow)))
  306.             if (0)
  307.             {
  308.                 SysBeep(7);
  309.                 return;
  310.             }
  311.             
  312.             if (TrackGoAway(theWindow, theEvent.where))
  313.                 DoTheCloseThing((WindowPeek)theWindow);        /* see menus.c */
  314.             break;
  315.         case inGrow:        /* grow box */
  316.             /* the accepted way to grow a window */
  317. //            if ((gInProgress) && (theWindow!=GetIndWindowPtr(kProgressWindow)))
  318.             if (0)
  319.             {
  320.                 SysBeep(7);
  321.                 return;
  322.             }
  323.             
  324.             if (thisWindowIsOurs)
  325.             {
  326.                 if (CallDispatchProc(theData, kGetGrowSize, (unsigned long)&sizeRect)==kFailure)
  327.                     sizeRect=qd.screenBits.bounds;
  328.             }
  329.             else sizeRect = qd.screenBits.bounds;
  330.             
  331.             windSize = GrowWindow(theWindow, theEvent.where, &sizeRect);
  332.             if (windSize != 0)
  333.             {
  334.                 GetPort(&oldPort);
  335.                 SetPort(theWindow);
  336.                 EraseRect(&theWindow->portRect);
  337.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  338.                 InvalRect(&theWindow->portRect);
  339.                 SetPort(oldPort);
  340.                 
  341.                 if (thisWindowIsOurs)    /* update window bounds in window data struct */
  342.                 {
  343.                     if (CallDispatchProc(theData, kGrow, 0L)==kFailure)
  344.                     {
  345.                         SetWindowData_windowBounds(theData, 
  346.                             (*(((WindowPeek)GetWindowPtr(theData))->contRgn))->rgnBBox);
  347.                         SetWindowData_windowPos(theData,
  348.                             GetWindowBoundsTopLeft((WindowDataHandle)theData));
  349.                         SetWindowData_windowPosInitted(theData, TRUE);
  350.                     }
  351.                 }
  352.             }
  353.             break;
  354.         case inZoomIn:        /* zoom box */
  355.         case inZoomOut:
  356.             /* the accepted way to track a zoom attempt */
  357. //            if ((gInProgress) && (theWindow!=GetIndWindowPtr(kProgressWindow)))
  358.             if (0)
  359.             {
  360.                 SysBeep(7);
  361.                 return;
  362.             }
  363.             
  364.             if (TrackBox(theWindow, theEvent.where, windowCode))
  365.             {
  366.                 GetPort(&oldPort);
  367.                 SetPort(theWindow);
  368.                 ZoomWindow(theWindow, windowCode, FALSE);
  369.                 InvalRect(&theWindow->portRect);
  370.                 SetPort(oldPort);
  371.             }
  372.             
  373.             if (thisWindowIsOurs)    /* update window bounds in window data struct */
  374.             {
  375.                 if (CallDispatchProc(theData, kZoom, 0L)==kFailure)
  376.                 {
  377.                     SetWindowData_windowBounds(theData, 
  378.                         (*(((WindowPeek)GetWindowPtr(theData))->contRgn))->rgnBBox);
  379.                     SetWindowData_windowPos(theData,
  380.                         GetWindowBoundsTopLeft((WindowDataHandle)theData));
  381.                     SetWindowData_windowPosInitted(theData, TRUE);
  382.                 }
  383.             }
  384.             break;
  385.     }
  386. }
  387.  
  388. void ShutDownEnvironment(Boolean fullShutdown)
  389. {
  390.     SaveThePrefs();
  391.     if (fullShutdown)
  392.     {
  393.         ShutDownTheProgram();        /* program-specific cleanup */
  394.         ShutDownTheGraphics();        /* shell-specific cleanup */
  395.     }
  396. }
  397.